package com.ruoyi.project.vehicle.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.site.controller.GaoDeMapController;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.vehicle.domain.BinsVehicleInfo;
import com.ruoyi.project.vehicle.service.IBinsVehicleInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 车辆信息Controller
 * 
 * @author Administrator
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/vehicle/info")
public class BinsVehicleInfoController extends BaseController
{
    @Autowired
    private IBinsVehicleInfoService binsVehicleInfoService;

    /**
     * 查询车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('vehicle:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(BinsVehicleInfo binsVehicleInfo)
    {
        if (binsVehicleInfo.getDeptId() == null){
            binsVehicleInfo.setDeptId(getCommonDeptId());
        }
        startPage();
        List<BinsVehicleInfo> list = binsVehicleInfoService.selectBinsVehicleInfoList(binsVehicleInfo);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('vehicle:info:export')")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BinsVehicleInfo binsVehicleInfo)
    {
        List<BinsVehicleInfo> list = binsVehicleInfoService.selectBinsVehicleInfoList(binsVehicleInfo);
        ExcelUtil<BinsVehicleInfo> util = new ExcelUtil<BinsVehicleInfo>(BinsVehicleInfo.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:info:query')")
    @GetMapping(value = "/{vehicleCode}")
    public AjaxResult getInfo(@PathVariable("vehicleCode") Long vehicleCode)
    {
        return success(binsVehicleInfoService.selectBinsVehicleInfoByVehicleCode(vehicleCode));
    }

    @Autowired
    GaoDeMapController gaoDeMapController;
    /**
     * 新增车辆信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:info:add')")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BinsVehicleInfo binsVehicleInfo)
    {
        if (binsVehicleInfo.getPoint()!= null){
            Map<String, String> address = gaoDeMapController.getGDAddress(binsVehicleInfo.getPoint());
            logger.info("address: " + address.get("address"));
            binsVehicleInfo.setAddress(address.get("address"));
        }
        return toAjax(binsVehicleInfoService.insertBinsVehicleInfo(binsVehicleInfo));
    }

    /**
     * 修改车辆信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:info:edit')")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BinsVehicleInfo binsVehicleInfo)
    {
        logger.info("binsVehicleInfo: " + binsVehicleInfo);
        if (binsVehicleInfo.getVehicleCodes() != null){
            int count = 0;

            for (Long vehicleCode : binsVehicleInfo.getVehicleCodes()) {
                binsVehicleInfo.setVehicleCode(vehicleCode);
                if (binsVehicleInfo.getPoint()!= null){
                    Map<String, String> address = gaoDeMapController.getGDAddress(binsVehicleInfo.getPoint());
                    logger.info("address: " + address.get("address"));
                    binsVehicleInfo.setAddress(address.get("address"));
                }
                count = binsVehicleInfoService.updateBinsVehicleInfo(binsVehicleInfo);
            }
            return AjaxResult.success();
        }
        if (binsVehicleInfo.getPoint()!= null){
            Map<String, String> address = gaoDeMapController.getGDAddress(binsVehicleInfo.getPoint());
            logger.info("address: " + address.get("address"));
            binsVehicleInfo.setAddress(address.get("address"));
        }
        return toAjax(binsVehicleInfoService.updateBinsVehicleInfo(binsVehicleInfo));
    }

    /**
     * 删除车辆信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:info:remove')")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleCodes}")
    public AjaxResult remove(@PathVariable Long[] vehicleCodes)
    {
        return toAjax(binsVehicleInfoService.deleteBinsVehicleInfoByVehicleCodes(vehicleCodes));
    }
}
